home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / Reference / ROM_Kernel_Manuals / Lib_examples / trap_c.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-20  |  2.3 KB  |  80 lines

  1. ;/* trap_c.c - Execute me to compile me with SAS C 5.10
  2. LC -b0 -cfistq -v -y -j73 trap_c.c
  3. Blink FROM LIB:c.o,trap_c.o,trap_a.o TO trap LIBRARY LIB:LC.lib,LIB:Amiga.lib
  4. quit
  5.  
  6. trap_c.c - C module of sample integer divide-by-zero trap
  7. */
  8. #include <exec/types.h>
  9. #include <exec/tasks.h>
  10. #include <clib/exec_protos.h>
  11. #include <stdlib.h>
  12. #include <stdio.h>
  13.  
  14. #ifdef LATTICE
  15. int CXBRK(void) { return(0); }  /* Disable Lattice CTRL/C handling */
  16. int chkabort(void) {return(0); }
  17. #endif
  18.  
  19. extern ULONG trapa();           /* assembler trap code in trap_a.asm */
  20.  
  21. APTR oldTrapCode;
  22. ULONG countdiv0;
  23.  
  24. void main(int argc, char **argv)
  25. {
  26.     struct Task *thistask;
  27.     ULONG k,j;
  28.  
  29.     thistask = FindTask(NULL);
  30.  
  31.     /* Save our task's current trap code pointer */
  32.     oldTrapCode = thistask->tc_TrapCode;
  33.  
  34.     /* Point task to our assembler trap handler code.  Ours will just count */
  35.     /* divide-by-zero traps, and pass other traps on to the normal TrapCode */
  36.     thistask->tc_TrapCode = (APTR)trapa;
  37.  
  38.     countdiv0 = 0L;
  39.  
  40.     for(k=0; k<4; k++)            /* Let's divide by zero a few times */
  41.        {
  42.        printf("dividing %ld by zero... ",k);
  43.        j = k/0L;
  44.        printf("did it\n");
  45.        }
  46.     printf("\nDivide by zero happened %ld times\n",countdiv0);
  47.  
  48.     thistask->tc_TrapCode = oldTrapCode;     /* Restore old trap code */
  49. }
  50.  
  51.  
  52.  
  53. * trap_a.asm - Example trap handling code (leaves D0 intact).  Entered
  54. * in supervisor mode with the following on the supervisor stack:
  55. *    0(sp).l = trap#
  56. *    4(sp) Processor dependent exception frame
  57.  
  58.     INCLUDE "exec/types.i"
  59.     INCLUDE "libraries/dos.i"
  60.  
  61.         XDEF _trapa
  62.         XREF _countdiv0
  63.         XREF _oldTrapCode
  64.  
  65.         CODE
  66. _trapa:                                 ; our trap handler entry
  67.         CMPI.L  #5,(SP)                 ; is this a divide by zero ?
  68.         BNE.S   notdiv0                 ; no
  69.         ADD.L   #1,_countdiv0           ; yes, increment our div0 count
  70. endtrap:
  71.         ADDQ    #4,SP                   ; remove exception number from SSP
  72.         RTE                             ; return from exception
  73. notdiv0:
  74.         TST.L   _oldTrapCode            ; is there another trap handler ?
  75.         BEQ.S   endtrap                 ; no, so we'll exit
  76.         MOVE.L  _oldTrapCode,-(SP)      ; yes, go on to old TrapCode
  77.         RTS                             ; jumps to old TrapCode
  78.  
  79.         END
  80.